package cn.jiyun.controller;

import cn.jiyun.entity.Student;
import cn.jiyun.service.StudentService;
import cn.jiyun.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    @RequestMapping("findAll")
    public String findAll(HttpServletRequest request){
        List<StudentVo> all = studentService.findAll();
        request.setAttribute("sList",all);
        return "show";
    }
    @RequestMapping("toAdd")
    public String toAdd(HttpServletRequest request){
        return "add";
    }

    @RequestMapping("add")
    public String add(Student student){
        studentService.insertStudent(student);
        return "redirect:findAll";
    }

    @RequestMapping("toEdit")
    public String toEdit(Integer sid,HttpServletRequest request){
        Student student=studentService.findStudentById(sid);
        request.setAttribute("student",student);
        return "edit";
    }

    @RequestMapping("edit")
    public String edit(Student student){
        studentService.updateStudent(student);
        return "redirect:findAll";
    }

    @RequestMapping("del")
    public String edit(Integer sid){
        studentService.deleteStudent(sid);
        return "redirect:findAll";
    }
}
